using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AntEater.ComponentModel.BuildElements;
using AntEater.ComponentModel;

namespace AntEater.ComponentModel.BuildElements.Design
{
    /// <summary>
    /// Provides a control that allows a user to edit text.
    /// </summary>
    public partial class TextTypeEditorUI : UserControl
    {

        /// <summary>
        /// Occurs when the user has completed editing.
        /// </summary>
        public event EventHandler EditComplete;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:BuildElementPropertyVisualizer"/> class.
        /// </summary>
        public TextTypeEditorUI(string text) {

            this._propertyValue = text;
            
            InitializeComponent();

            editToolStrip.ImageList = editToolStripImageList;
            applyToolStripButton.ImageKey = "Apply";
            cancelToolStripButton.ImageKey = "Cancel";

            this.RefreshPropertyValueTextBox();
        }

        private string _propertyValue;

        /// <summary>
        /// Gets or sets the property value.
        /// </summary>
        /// <value>The property value.</value>
        public string PropertyValue {
            get { return _propertyValue; }
            protected set {
                _propertyValue = value;
                this.RefreshPropertyValueTextBox();
            }
        }

        private bool _isDirty;

        /// <summary>
        /// Gets or sets a value indicating whether this instance is dirty.
        /// </summary>
        /// <value><c>true</c> if this instance is dirty; otherwise, <c>false</c>.</value>
        public bool IsDirty {
            get { return _isDirty; }
            set { 
                _isDirty = value;
                applyToolStripButton.Enabled = _isDirty;
            }
        }

        /// <summary>
        /// Refreshes the property value text box.
        /// </summary>
        public void RefreshPropertyValueTextBox() {
            propertyValueTextBox.TextChanged -= new EventHandler(PropertyValueChanged);

            propertyValueTextBox.Clear();
            this.IsDirty = false;

            this.propertyValueTextBox.Text = this.PropertyValue;

            propertyValueTextBox.TextChanged += new EventHandler(PropertyValueChanged);
            
            base.Refresh();
        }

        /// <summary>
        /// Called when [edit complete].
        /// </summary>
        protected void OnEditComplete(){
            if (this.EditComplete != null) {
                this.EditComplete(this, new EventArgs());
            }
        }

        /// <summary>
        /// Handles the PropertyChanged Event of the Control which visualizes the Property.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void PropertyValueChanged(object sender, EventArgs e) {
            this.IsDirty = true;
        }

        /// <summary>
        /// Applies the property value changes.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void ApplyPropertyValueChanges(object sender, EventArgs e) {
            this.PropertyValue = this.propertyValueTextBox.Text;
            this.OnEditComplete();
        }

        /// <summary>
        /// Cancels the property value changes.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void CancelPropertyValueChanges(object sender, EventArgs e) {
            this.OnEditComplete();
        }
    }
}
